<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Art;
use App\Cat;
use App\User;
use App\Comm;
use App\Dsp;
use App\Dz;
use App\Sc;

class PronController extends Controller
{
    //收藏状态，1.收藏点赞过了，2收藏点赞成功
    const stored_success=2;
    const stored_ed=1;
    const dz_success=2;
    const dz_ed=1;

public function pro(Request $req,$id){
             //页面主区域的内容
            $art=Art::where('aid',$id)->first();
            $cat=Cat::where('cid',$art->cid)->first();
            $auth=User::where('uid',$art->uid)->first();

            //评论明细展示
            $comm=Comm::where('aid',$id)->orderBy('cmid','desc')->paginate(1);
            //评论数展示
            $dsp=Dsp::where('aid',$id)->first();
            

            //获取所有用户信息，得到评论中的用户头像和名字
            $users=User::get();
            //用户信息获取,和对该文章是否已经点赞收藏
            if($user=$req->user()){
                //判断是否已经点赞
                if(Dz::where('aid',$id)->where('uid',$user->uid)->first()){
                    $user->dz=1;    
                }else{
                    $user->dz=0;
                }
                //判断是否已经收藏
                if(Sc::where('aid',$id)->where('uid',$user->uid)->first()){
                    $user->sc=1;    
                }else{
                    $user->sc=0;
                }
            }else{
                $user=0;
            }
    return view('pro',['art'=>$art,'cat'=>$cat,'user'=>$user,'auth'=>$auth,'comm'=>$comm,'dsp'=>$dsp,'users'=>$users]);
   }
   public function propost(Request $req){
      //评论详表的存储
         $comm=new Comm();
         $comm->aid=$req->aid;
         $comm->uid=$req->uid;
         $comm->content=$req->content;
         $comm->pubtime=time();
         $comm->save();
         //点赞，收藏，评论表的存储
         $dps=Dsp::where('aid',$req->aid)->first();
         if($dps){
            $dps->increment('pl',1);
         }else{
            $dsp=new Dsp();
            $dsp->aid=$req->aid;
            $dsp->pl=1;
            $dsp->save();
         }
         return redirect("/pro/$req->aid");          
   }
   //点赞模块
   public function dz(Request $req){
    //没登陆先让登录
     if(!($req->aid!=0&&$req->uid!=0)){
            return '请先登录';
        }
        //点赞详表的内容
         $dz=Dz::where('aid','=',$req->aid)->where('uid','=',$req->uid)->first();
         if(!$dz){
            $dz=new Dz();
            $dz->aid=$req->aid;
            $dz->uid=$req->uid;
            $dz->dz=1;
            $dz->dztime=time();
            $dz->save();
         }elseif($dz->dz==1){
           $data=array('st'=>$this::dz_ed);
            return $data;
         }elseif($dz->dz==0){
            $dz->increment('dz',1);
         }

        //点赞，收藏，评论表的存储
             $dps=Dsp::where('aid',$req->aid)->first();
             if($dps){
                $dps->increment('dz',1);
             }else{
                $dsp=new Dsp();
                $dsp->aid=$req->aid;
                $dsp->dz=1;
                $dsp->save();
             }
        $dps=Dsp::where('aid',$req->aid)->first();
        //st为状态内容去类首看
        $data=array('dz'=>$dps->dz,'st'=>$this::dz_success);
         return $data;

   }

   //收藏模块
    public function sc(Request $req){
    //没登陆先让登录
     if(!($req->aid!=0&&$req->uid!=0)){
            return '请先登录';
        }
    //state=1为点赞，2为收藏 
        //收藏详表的内容
         $sc=Sc::where('aid','=',$req->aid)->where('uid','=',$req->uid)->first();
         if(!$sc){
            $sc=new Sc();
            $sc->aid=$req->aid;
            $sc->uid=$req->uid;
            $sc->sc=1;
            $sc->sctime=time();
            $sc->save();
         }elseif($sc->sc==1){
            //st为状态内容去类首看
            $data=array('st'=>$this::stored_ed);
            return  $data;
         }elseif($sc->sc==0){
            $dz->increment('sc',1);
         }

        //点赞，收藏，评论表的存储
             $dps=Dsp::where('aid',$req->aid)->first();
             if($dps){
                $dps->increment('sc',1);
             }else{
                $dsp=new Dsp();
                $dsp->aid=$req->aid;
                $dsp->sc=1;
                $dsp->save();
             }
             $dps=Dsp::where('aid',$req->aid)->first();
             //st为状态内容去类首看
             $data=array('sc'=>$dps->sc,'st'=>$this::stored_success);
             
         return $data;
   }


   public function pl(Request $req){
      //评论详表的存储
         $comm=new Comm();
         $comm->aid=$req->aid;
         $comm->uid=$req->uid;
         $comm->content=$req->content;
         $comm->pubtime=time();
         $comm->save();
        //点赞，收藏，评论表的存储
         $dps=Dsp::where('aid',$req->aid)->first();
         if($dps){
            $dps->increment('pl',1);
         }else{
            $dsp=new Dsp();
            $dsp->aid=$req->aid;
            $dsp->pl=1;
            $dsp->save();
         }

         //评论明细展示
        $comm=Comm::where('aid',$req->aid)->orderBy('cmid','desc')->paginate(1);
        $pl=Dsp::where('aid',$req->aid)->first()->pl;
        $users=User::get();
        $user=User::where('uid',$req->uid)->first();
        $a="<h2 class=".'"'."comment-title".'"'." >影片点评<span class=".'"'."num".'"'." id=".'"'."yydp".'"'.">已有{$pl}条点评</span> </h2>";
        foreach($comm as $v){
            $a.="<ul class=".'"'."com-list".'"'." id=".'"'."com-list".'"'."><li class=".'"'."clearfix".'"'." id=".'"'."c_297598".'"'."><div class=".'"'."com-prove ".'"'." data-commentid=".'"'."297598".'"'." approved=".'"'."undefined".'"'.">10</div>
            <div class=".'"'."com-img".'"'.">";
            foreach($users as $u){
                if($u->uid==$v->uid){         
                $a.="<a target=".'"'."_blank".'"'." href=".'"'."{$u->head_path}".'"'." class=".'"'."full".'"'."><img class=".'"'."full".'"'."  alt=".'"'."头像".'"'."></a></div><div class=".'"'."com-text".'"'."><h4 class=".'"'."title".'"'."><a target=".'"'."_blank".'"'." >{$u->name}</a></h4>";
                }
            }
            $a.="<div class=".'"'."intro".'"'.">".$v->content."</div><div class=".'"'."ope".'"'."><span class=".'"'."time".'"'.">".date('Y-m-d',$v->pubtime)."</span></div></div></li><li class=".'"'."com-sub clearfix".'"'."></li></ul>";
        }
        $a.="<div class=".'"'."comment-pager".'"'." id=".'"'."comment-pager".'"'." >".$comm->render()."</div>";





        

        return  $a;
   }
   public function dlpl(Request $req){
    return redirect("/pro/$req->aid");
   }

}
